﻿using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Threading;

namespace LeafBookmark.ViewModels
{
    class PropertiesViewModelBase : DispatcherObject, INotifyPropertyChanged
    {

        private PropertyChangedEventHandler propertyChangedEvent;
        /// <summary>
        /// PropertyChanged event for INotifyPropertyChanged implementation.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged
        {
            add
            {
                VerifyCalledOnUIThread();
                propertyChangedEvent += value;
            }
            remove
            {
                VerifyCalledOnUIThread();
                propertyChangedEvent -= value;
            }
        }

        /// <summary>
        /// Utility function for use by subclasses to notify that a property has changed.
        /// </summary>
        /// <param name="propertyName">The name of the property.</param>
        protected void RaisePropertyChanged(string propertyName)
        {
            VerifyCalledOnUIThread();
            if (propertyChangedEvent != null)
            {
                propertyChangedEvent(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        [Conditional("Debug")]
        protected void VerifyCalledOnUIThread()
        {
            Debug.Assert(CheckAccess(), "Call must be made on UI thread.");
        }

    }

}
